/**
 * tzdesk系统平台
 * moovip_admin
 * com.tz.web
 * IndexController.java
 * 创建人:xuchengfei 
 * 时间：2016年11月23日-下午11:46:36
 *
 */
package com.tz.web;

import com.tz.bean.AdminUser;
import com.tz.bean.Permission;
import com.tz.bean.TzParams;
import com.tz.core.BaseController;
import com.tz.service.adminUser.IAdminUserService;
import com.tz.service.role.IRoleService;
import com.tz.util.TmStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

import static com.tz.util.TzConstant.*;

/**
 * 
 * IndexController
 * 创建人:xuchengfei 
 * 时间：2016年11月23日-下午11:46:36
 * @version 1.0.0
 * 
 */
@Controller
public class LoginController extends BaseController{
	
	@Autowired
	private IAdminUserService service;

	@Autowired
	private IRoleService roleService;

	@RequestMapping("/login")
	public String login(String returnUrl ,Model model){
		System.out.println("returnUrl=="+returnUrl);
		if(TmStringUtils.isNotEmpty(returnUrl)) {
			returnUrl =returnUrl.replace("null","") ;
		}
		model.addAttribute("returnUrl" , returnUrl);
		return "login";
	}

	/**
	 * 登陆方法
	 * com.tz.web 
	 * 方法名：logined
	 * 创建人：xuchen`gfei
	 * 手机号码:15074816437
	 * 时间：2016年11月24日-上午1:13:34
	 * @param params
	 * @return 
	 * 返回类型：String
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value="/logined",method=RequestMethod.POST)
	public String logined(TzParams params){
		if(params!=null){
			if(TmStringUtils.isNotEmpty(params.getAccount()) && TmStringUtils.isNotEmpty(params.getPassword())){
				params.setPassword(TmStringUtils.md5Base64(params.getPassword()));
				AdminUser user = service.getLogin(params);
				if(user!=null){
					if(TmStringUtils.isEmpty(user.getForbidenCode()) || "002".equals(user.getForbidenCode())){
						return "forbiden";
					}else{
						HashMap<String, Object> roleMap = roleService.findRolesByUserId(user.getId());
						//得到所有的权限
						List<Permission> allPermissions = service.getPermissionByUserId(user.getId());
						session.setAttribute(SESSION_ALL_PERMISSION, allPermissions);
						//得到一级权限
						List<Permission> firstPermissions = service.getParentPermissionByUserId(user.getId());
						//将二级权限放入一级权限中
					    for(Permission permission:firstPermissions){
							HashMap map = new HashMap();
							map.put("id",user.getId());
							map.put("pid",permission.getId());
							List<Permission> children = service.getNextPermissionByUserId(map);
							permission.setPermissions(children);
						}
						session.setAttribute(SESSION_MENU_PERMISSION, firstPermissions);
						session.setAttribute(SESSION_USER, user);
						session.setAttribute(SESSION_USER_ROLE, roleMap);
						session.setAttribute(SESSION_USER_USERNAME, user.getUsername());
						//日记监控用户行为和获取请求参数
						request.getServletContext().setAttribute("user_log", user);
						request.getServletContext().setAttribute("request_log", request);
						return "success";
					}
				}else{
					return "fail";
				}
			}else{
				return "null";//请输入账号和密码
			}
		}else{
			return "error";
		}
	}

	@RequestMapping("/logout")
	public String logout(){
		session.invalidate();
		return "redirect:login";
	}
}